package com.jie.langlangmall.portal.service.impl;

import com.jie.langlangmall.mapper.PmsProductMapper;
import com.jie.langlangmall.model.PmsProduct;
import com.jie.langlangmall.model.UmsMember;
import com.jie.langlangmall.portal.domain.MemberReadHistory;
import com.jie.langlangmall.portal.repository.MemberReadHistoryRepository;
import com.jie.langlangmall.portal.service.MemberReadHistoryService;
import com.jie.langlangmall.portal.service.UmsMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author Jie
 * @Date 2024/11/27
 * @Email 3217998214@qq.com
 * @Description 浏览记录
 */
@Service
public class MemberReadHistoryServiceImpl implements MemberReadHistoryService {

    @Autowired
    private PmsProductMapper productMapper;
    @Autowired
    private MemberReadHistoryRepository memberReadHistoryRepository;
    @Autowired
    private UmsMemberService memberService;

    /**
     * 添加浏览记录
     *
     * @param memberReadHistory
     */
    @Override
    public void create(MemberReadHistory memberReadHistory) {
        if (memberReadHistory.getProductId() == null) {
            return;
        }
        UmsMember member = memberService.getCurrentMember();
        memberReadHistory.setMemberId(member.getId());
        memberReadHistory.setMemberNickname(member.getNickname());
        memberReadHistory.setMemberIcon(member.getIcon());
        memberReadHistory.setId(null);
        memberReadHistory.setCreateTime(new Date());
        PmsProduct product = productMapper.selectByPrimaryKey(memberReadHistory.getProductId());
        if (product == null || product.getDeleteStatus() == 1) {
            return;
        }
        memberReadHistory.setProductName(product.getName());
        memberReadHistory.setProductSubTitle(product.getSubTitle());
        memberReadHistory.setProductPrice(product.getPrice() + "");
        memberReadHistory.setProductPic(product.getPic());
        memberReadHistoryRepository.save(memberReadHistory);
    }

    @Override
    public void delete(List<String> ids) {
        List<MemberReadHistory> deleteList = new ArrayList<>();
        for (String id : ids) {
            MemberReadHistory memberReadHistory = new MemberReadHistory();
            memberReadHistory.setId(id);
            deleteList.add(memberReadHistory);
        }
        memberReadHistoryRepository.deleteAll(deleteList);
    }

    @Override
    public void clear() {
        UmsMember member = memberService.getCurrentMember();
        memberReadHistoryRepository.deleteAllByMemberId(member.getId());
    }

    @Override
    public Page<MemberReadHistory> list(Integer pageNum, Integer pageSize) {
        UmsMember member = memberService.getCurrentMember();
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        return memberReadHistoryRepository.findByMemberIdOrderByCreateTimeDesc(member.getId(), pageable);
    }
}
